# -*- coding: utf-8 -*-

# File Name： center_point
# Description :
# Author : lirui
# create_date： 2022/6/6
# Change Activity:
import cv2
import numpy as np

from .convex_hull import get_convex_hull


def get_gravity_center_point(contours):
    """
    get gravity center point
    Args:
        contours:

    Returns:

    """
    points = contours.copy()
    convex_hull_points = get_convex_hull(points)
    center_point = get_contour_center_point(convex_hull_points)  # center point of convex hull points
    return center_point, convex_hull_points


def get_contour_center_point(ori_contour):
    """

    Args:
        ori_contour:

    Returns:

    """
    if ori_contour.shape[-1] != 2:
        contour = ori_contour.reshape(-1, 2)
    else:
        contour = ori_contour.copy()
    ws = contour[..., 0]
    hs = contour[..., 1]
    center_point = np.array([ws.mean(), hs.mean()])
    return center_point


def get_box_edge_center_point(box):
    """
    find the center point of every edge.
    Args:
        box:(4,2)

    Returns:

    """
    p0 = np.mean(box[[0, 1], :], axis=0)
    p1 = np.mean(box[[1, 2], :], axis=0)
    p2 = np.mean(box[[2, 3], :], axis=0)
    p3 = np.mean(box[[3, 0], :], axis=0)
    return np.array([p0, p1, p2, p3])


def enlarge_bbox(bbox, enlarge_width, enlarge_height):
    """

    Args:
        bbox: (4,2)
        enlarge_width:
        enlarge_height:

    Returns:

    """
    center_point, (w, h), theta = cv2.minAreaRect(bbox)
    edge = (w + enlarge_width, h + enlarge_height)
    box = cv2.boxPoints((center_point, edge, theta))
    return box
